﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using RHD.Common.Web.Components.AutoComplete;
using RHD.Common.Web.Components.Grid;
using RHD.Service;
using RHD.Web.Infrastructure;
using RHD.Web.Models;

namespace RHD.Web.Controllers {
	public class HomeController : BaseController {

		#region Services

		public IUsersService UsersService { get; set; }

		#endregion

		#region Actions

		public ActionResult Index() {
			TestModel model = this.GetTestVM();
			return View(model);
		}

		#region AutoComplete

		public AutoCompleteResult AutoCompleteResult(string value) {
			IList<TestAutoCompleteModel> list = new List<TestAutoCompleteModel>();
			list.Add(new TestAutoCompleteModel() { Text = "Abacaxi", Value = 1 });
			list.Add(new TestAutoCompleteModel() { Text = "Uva", Value = 2 });
			list.Add(new TestAutoCompleteModel() { Text = "Pera", Value = 3 });
			list.Add(new TestAutoCompleteModel() { Text = "Maça", Value = 4 });
			list.Add(new TestAutoCompleteModel() { Text = "Banana", Value = 5 });
			return new AutoCompleteResult(list, "Value", "Text");
		}

		#endregion

		#region Grid

		public GridResult GridResult(TestFilterModel Filter) {
			IList<TestGridModel> list = new List<TestGridModel>();
			list.Add(new TestGridModel() { ID = 1, Nome = "Renato Henrique Dias", Altura = 1.79, Idade = 24, Salario = 100 });
			list.Add(new TestGridModel() { ID = 2, Nome = "Tiago Dias", Altura = 1.84, Idade = 23, Salario = 200 });
			list.Add(new TestGridModel() { ID = 3, Nome = "Valquiria Munhoz", Altura = 1.60, Idade = 24, Salario = 300 });
			list.Add(new TestGridModel() { ID = 4, Nome = "Gustavo Neves", Altura = 1.65, Idade = 26, Salario = 400 });
			list.Add(new TestGridModel() { ID = 5, Nome = "Marcos Roberto", Altura = 1.60, Idade = 49, Salario = 500 });

			if (!string.IsNullOrEmpty(Filter.Nome))
				list = list.Where(d => d.Nome.Contains(Filter.Nome)).ToList();

			return new GridResult(list);
		}

		public ActionResult GridEdit(int id) {
			this.AddMsgSuccess(string.Format("Editei a linha {0}.", id));
			return View("Index", this.GetTestVM());
		}

		public ActionResult GridDelete(int id) {
			this.AddMsgSuccess(string.Format("Deletei a linha {0}", id));
			return View("Index", this.GetTestVM());
		}

		#endregion

		#endregion

		#region Private Members

		private TestModel GetTestVM() {
			TestModel model = new TestModel();
			model.AutoComplete = this.GetAutoCompleteVM();
			model.AutoCompleteDemand = this.GetAutoCompleteVM();
			return model;
		}

		private TestAutoCompleteModel GetAutoCompleteVM() {
			TestAutoCompleteModel model = new TestAutoCompleteModel();
			model.Source.Add(new TestAutoCompleteModel() { Text = "Renato Dias", Value = 1 });
			model.Source.Add(new TestAutoCompleteModel() { Text = "Valquiria Munhoz", Value = 2 });
			model.Source.Add(new TestAutoCompleteModel() { Text = "Tiago Henrique", Value = 3 });
			model.Source.Add(new TestAutoCompleteModel() { Text = "Gustavo Neves", Value = 4 });
			return model;
		}

		#endregion
	}
}
